// Call the dataTables jQuery plugin
$(document).ready(function () {
    $('#dataTable').DataTable();
    $('#dataTable1').DataTable();
    $('#dataTable2').DataTable();
});


// 添加国家或区域
$("#addCountryBtn").click(function () {
    var tInput = $("#addCountryName");
    if (tInput.val()) {
        $.ajax({
            url: ctxPath + "admin/addCountry",
            type: 'post',
            data: {"countryName": tInput.val()},
            dataType: 'json',
            success: function (data) {
                if (data["result"] == 1) {
                    showMsg("添加成功", 'alert-success');
                    location.reload();
                }
            }
        });
    } else {
        showMsg("名称不可为空", 'alert-warning');
    }
})

// 删除国家或区域
function deleteCountry(countryID) {
    $.ajax({
        url: ctxPath + "admin/deleteCountry/" + countryID,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data["result"] == 1) {
                showMsg("删除成功", 'alert-success');
                location.reload();
            }
        }
    });
}

// 修改国家或区域
function modifyCountry(countryID, btn) {
    modifyName = $("#modifyCountryName" + countryID).val();
    if (typeof (modifyName) == "undefined") {
        var tmpEl = $("#countryName" + countryID);
        var t = tmpEl.text();
        tmpEl.empty();
        tmpEl.append("<div class='row'>" +
            "           <input id='modifyCountryName" + countryID + "' type='text' class='col-8 form-control form-text' placeholder='名称' value='" + t + "'/>" +
            "           <span class='col-1'></span>" +
            "           <span  class='col-2'>" +
            "              <button onclick='JavaScript:location.reload()' class='btn-sm btn-info'><i class='fas fa-times'></i></button>\n" +
            "           </span></div>");
        $('i', $(btn)).attr("class", "fas fa-check");
    } else {
        if (modifyName) {
            $.ajax({
                url: ctxPath + "admin/updateCountry",
                type: 'post',
                data: {"countryID": countryID, "countryName": modifyName},
                dataType: 'json',
                success: function (data) {
                    if (data["result"] == 1) {
                        showMsg("修改成功", 'alert-success');
                        location.reload();
                    }
                }
            });
        } else {
            showMsg("名称不可为空", 'alert-warning');
        }
    }
}

//--------------------------

// 添加城市
$("#addCityBtn").click(function () {
    var cityID = $("#addCityID").val();
    var cityName = $("#addCityName").val();
    var countryID = $("#addCityCountryID").val();
    if (countryID && cityName && countryID) {
        $.ajax({
            url: ctxPath + "admin/addCity",
            type: 'post',
            data: {"cityID": cityID, "cityName": cityName, "countryID": countryID},
            dataType: 'json',
            success: function (data) {
                if (data["result"] == 1) {
                    showMsg("添加成功", 'alert-success');
                    location.reload();
                }
            }
        });
    } else {
        showMsg("录入项不可为空", 'alert-warning');
    }
})

// 删除城市
function deleteCity(cityID) {
    $.ajax({
        url: ctxPath + "admin/deleteCity/" + cityID,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data["result"] == 1) {
                showMsg("删除成功", 'alert-success');
                location.reload();
            }
        }
    });
}

// 修改城市
function modifyCity(cityID, countryID, btn) {
    modifyName = $("#modifyCityName" + cityID).val();
    if (typeof (modifyName) == "undefined") {
        var tmpNameEl = $("#cityName" + cityID);
        var cityName = tmpNameEl.text();
        tmpNameEl.empty();
        tmpNameEl.append(
            "<input id='modifyCityName" + cityID + "' type='text' class='col-8 form-control form-text' placeholder='名称' value='" + cityName + "'/>"
        );

        var tmpcityCountryNameEl = $("#cityCountryName" + cityID);
        var countryName = tmpcityCountryNameEl.text();
        tmpcityCountryNameEl.empty();
        var option = $("#addCityCountryID").html();

        tmpcityCountryNameEl.append("<div class='row'>" +
            "           <select id='modifyCityCountryName' class='col-8 form-control'>" + option + "</select>" +
            "           <span  class='col-2'>" +
            "              <button onclick='JavaScript:location.reload()' class='btn-sm btn-info'><i class='fas fa-times'></i></button>\n" +
            "           </span></div>");

        $("#modifyCityCountryName").val(countryID);
        $('i', $(btn)).attr("class", "fas fa-check");

    } else {
        if (modifyName) {
            $.ajax({
                url: ctxPath + "admin/updateCity",
                type: 'post',
                data: {"cityID": cityID, "cityName": modifyName, "countryID": $("#modifyCityCountryName").val()},
                dataType: 'json',
                success: function (data) {
                    if (data["result"] == 1) {
                        showMsg("修改成功", 'alert-success');
                        location.reload();
                    }
                }
            });
        } else {
            showMsg("名称不可为空", 'alert-warning');
        }
    }
}

//----------------------------------------------

// 添加机场
$("#addAirportBtn").click(function () {
    var airportID = $("#addAirportID").val();
    var airportName = $("#addAirportName").val();
    var cityID = $("#addAirportCityID").val();
    if (airportID && airportName && cityID) {
        $.ajax({
            url: ctxPath + "admin/addAirport",
            type: 'post',
            data: {"airportID": airportID, "airportName": airportName, "cityID": cityID},
            dataType: 'json',
            success: function (data) {
                if (data["result"] == 1) {
                    showMsg("添加成功", 'alert-success');
                    location.reload();
                }
            }
        });
    } else {
        showMsg("录入项不可为空", 'alert-warning');
    }
})


// 删除机场
function deleteAirport(airportID) {
  $.ajax({
    url: ctxPath+"admin/deleteAirport/"+airportID,
    type:'get',
    dataType:'json',
    success:function(data){
      if(data["result"]==1){
        showMsg("删除成功",'alert-success');
        location.reload();
      }
    }
  });
}

// 修改机场
function modifyAirport(airportID, cityID, btn) {
    modifyName = $("#modifyAirportName" + airportID).val();

    if (typeof (modifyName) == "undefined") {
        var tmpNameEl = $("#airportName" + airportID);
        var airportName = tmpNameEl.text();
        tmpNameEl.empty();
        tmpNameEl.append(
            "<input id='modifyAirportName" + airportID + "' type='text' class='col-8 form-control form-text' placeholder='名称' value='" + airportName + "'/>"
        );

        var tmpAirportCityNameEl = $("#airportCityName" + airportID);
        var cityName = tmpAirportCityNameEl.text();
        tmpAirportCityNameEl.empty();
        var option = $("#addAirportCityID").html();

        tmpAirportCityNameEl.append("<div class='row'>" +
            "           <select id='modifyAirportCityName' class='col-8 form-control'>" + option + "</select>" +
            "           <span  class='col-2'>" +
            "              <button onclick='JavaScript:location.reload()' class='btn-sm btn-info'><i class='fas fa-times'></i></button>\n" +
            "           </span></div>");

        $("#modifyAirportCityName").val(cityID);
        $('i', $(btn)).attr("class", "fas fa-check");

    } else {
        if (modifyName) {
            $.ajax({
                url: ctxPath + "admin/updateAirport",
                type: 'post',
                data: {"airportID": airportID, "airportName": modifyName, "cityID": $("#modifyAirportCityName").val()},
                dataType: 'json',
                success: function (data) {
                    if (data["result"] == 1) {
                        showMsg("修改成功", 'alert-success');
                        location.reload();
                    }
                }
            });
        } else {
            showMsg("名称不可为空", 'alert-warning');
        }
    }
}


//-------------------------------
// 添加航站楼
function addTerminal(airportID) {
    $("#tmpModal").modal('show');
    $("#tmpModalTitle").text('添加航站楼');
    var tbody = $("#tmpModalBody");
    tbody.empty();
    tbody.append("<input id='addTerminalName' type='text' class='col-8 form-control form-text' placeholder='名称'/>");
    if($("#tmpModalFooter").children().length==1)
        $("#tmpModalFooter").prepend("<button id='addTerminalBtn' class='btn btn-primary' type='button'>添加</button>");

    $("#addTerminalBtn").click(function () {
        var terminalName = $("#addTerminalName").val();
        if(terminalName){
            $.ajax({
                url: ctxPath + "admin/addTerminal",
                type: 'post',
                data: {"terminalName": terminalName, "airportID":airportID},
                dataType: 'json',
                success: function (data) {
                    if (data["result"] == 1) {
                        showMsg("添加成功", 'alert-success');
                        location.reload();
                    }
                }
            });
        }else {
            showMsg("名称不可为空", 'alert-warning');
        }
    })
}

// 删除航站楼
function deleteTerminal(terminalID) {
    $.ajax({
        url: ctxPath + "admin/deleteTerminal/" + terminalID,
        type: 'get',
        dataType: 'json',
        success: function (data) {
            if (data["result"] == 1) {
                showMsg("删除成功", 'alert-success');
                location.reload();
            }
        }
    });
}

// 修改航站楼
function modifyTerminal(terminalID, terminalName) {
    $("#tmpModal").modal('show');
    $("#tmpModalTitle").text('修改航站楼信息');
    var tbody = $("#tmpModalBody");
    tbody.empty();
    tbody.append("<input id='modifyTerminalName' type='text' class='col-8 form-control form-text' placeholder='名称' value='"+terminalName+"'/>");
    if($("#tmpModalFooter").children().length==1)
        $("#tmpModalFooter").prepend("<button id='modifyTerminalBtn' class='btn btn-primary' type='button'>修改</button>");
    $("#modifyTerminalBtn").click(function () {
        var modifyName = $("#modifyTerminalName").val();
        if (modifyName) {
            $.ajax({
                url: ctxPath + "admin/updateTerminal",
                type: 'post',
                data: {"terminalID": terminalID, "terminalName": modifyName},
                dataType: 'json',
                success: function (data) {
                    if (data["result"] == 1) {
                        showMsg("修改成功", 'alert-success');
                        location.reload();
                    }
                }
            });
        } else {
            showMsg("名称不可为空", 'alert-warning');
        }
    })
}

// 显示提示框
function showMsg(msg, style, time) {
    style = (style === undefined) ? 'alert-success' : style;
    time = (time === undefined) ? 1200 : time;
    $('.alert').html(msg).addClass(style).show().delay(time).fadeOut();
}